﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ERMap;

namespace PhoneBook.Manage
{
    public class sysModuleRole
    {
        public static void add(string moduleid, string roleid ,string rightflag)
        {
            Hashtable sysmodulerole = new Hashtable();
            sysmodulerole["moduleid"] = moduleid;
            sysmodulerole["roleid"] = roleid;
            sysmodulerole["rightflag"] = rightflag;
            ERMapHelper.setmodel("sysmodulerole", "moduleid,roleid", sysmodulerole);
        }

        public static void add(string moduleid, string roleid)
        {
            add(moduleid, roleid, "1");
        }

        public static void del(string moduleid, string roleid)
        {
            Hashtable sysmodulerole = new Hashtable();
            sysmodulerole["moduleid"] = moduleid;
            sysmodulerole["roleid"] = roleid;
            ERMapHelper.destroy("sysmodulerole", sysmodulerole);
        }

        public static DataSet get(string roleid,string usertype)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select psm.modulename as pmodulename,sm.*,smr.* ");
            sb.Append(" from sysModule sm ");
            sb.Append(" inner join sysModule psm on  sm.parentid = psm.moduleid");
            sb.Append(" left outer join sysModuleRole smr on sm.moduleid = smr.moduleid and isnull(smr.roleid,@roleid) = @roleid");
            sb.Append(" where sm.usertype = @usertype and sm.parentid is not null");
            sb.Append(" and ISNULL(sm.isdel,0) = 0");
            sb.Append(" and ISNULL(psm.isdel,0) = 0");
            sb.Append(" and ISNULL(smr.isdel,0) = 0");

            Hashtable param = new Hashtable();
            param["roleid"] = roleid;
            param["usertype"] = usertype;

            return ERMapHelper.getCustomData(sb.ToString(), param);
        }

        public static DataSet get(string roleid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select psm.modulename as pmodulename,sm.*,smr.* ");
            sb.Append(" from sysModule sm ");
            sb.Append(" inner join sysModule psm on  sm.parentid = psm.moduleid");
            sb.Append(" left outer join sysModuleRole smr on sm.moduleid = smr.moduleid and isnull(smr.roleid,@roleid) = @roleid");
            sb.Append(" where sm.parentid is not null");
            sb.Append(" and ISNULL(sm.isdel,0) = 0");
            sb.Append(" and ISNULL(psm.isdel,0) = 0");
            sb.Append(" and ISNULL(smr.isdel,0) = 0");

            Hashtable param = new Hashtable();
            param["roleid"] = roleid;

            return ERMapHelper.getCustomData(sb.ToString(), param);
        }
    }
}
